﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Red
{
    public partial class Frm : Form
    {
        public Frm()
        {
            InitializeComponent();
        }

        private static string hongbaoInputnameUrl = "http://hongbao.app.baidu.com/hongbao/inputname";

        private static string guiZe = "http://chunjie.baidu.com/guize/";

        private static string fileName = "namebir.log";

        private void Frm_Load(object sender, EventArgs e)
        {
            this.Text = "搜红包-";// + "我搜红包获奖后，不小心关闭了页面或断网了怎么办？ 我们会保留您的获奖信息。您可以在一个小时内，输入与上次搜索相同的个人信息和生日，重新参与活动，您将直接进入获奖页面!";
            object o = VoteLog.GetLogs(fileName);
            if (o != null)
            {
                try
                {
                    string[] str = o.ToString().Split('\n');
                    if (str.Length >= 2)
                    {
                        this.txtUserName.Text = str[0].Split(':')[1];
                        this.dtpBir.Value = Convert.ToDateTime(str[1].Split(':')[1]);
                    }
                }
                catch (Exception)
                {
                    this.txtUserName.Text = "你的姓名";
                    this.dtpBir.Value = Convert.ToDateTime("1990-06-01");
                }
            }
            else
            {
                this.txtUserName.Text = "你的姓名";
                this.dtpBir.Value = Convert.ToDateTime("1990-06-01");
            }
            this.timSubmit.Interval = 100;
            this.timer1.Interval = 120;
            this.tsslblInfo.Text = "Enter进行下一次";
            try
            {
                this.wb.Url = new Uri(guiZe);
            }
            catch (Exception)
            {

            }
        }
        //开始
        private void btnStart_Click(object sender, EventArgs e)
        {
            this.btnStart.Text = "下一次";
            Start();
        }

        private void Start()
        {
            try
            {
                //this.wb.Document.OpenNew(true);
                this.wb.Url = new Uri(hongbaoInputnameUrl);
                while (this.wb.ReadyState < WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();
                }
                HtmlDocument doc = this.wb.Document;
                var test = doc.GetElementById("captcha");



                doc.GetElementById("captcha").Focus();//验证码
                doc.GetElementById("div-name").SetAttribute("value", this.txtUserName.Text.Trim());//用户名
                doc.GetElementById("check-1").SetAttribute("checked", "checked");//条款
                string birYear = this.dtpBir.Value.Year.ToString();
                int birMonth = this.dtpBir.Value.Month;
                int birDay = this.dtpBir.Value.Day;
                //生日
                doc.GetElementById("select-year").SetAttribute("value", birYear);
                doc.GetElementById("select-month").SetAttribute("value", birMonth <= 9 ? "0" + birMonth : birMonth.ToString());
                doc.GetElementById("select-day").SetAttribute("value", birDay <= 9 ? "0" + birDay : birDay.ToString());
                this.timer1.Enabled = true;
                this.timSubmit.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        //加载 完成 时 发生
        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //var  s= sender as WebBrowser;
            //s.Document.GetElementById("captcha");
            //if (s!=null)
            //{
            //    s.Focus();
            //}
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                this.wb.Document.GetElementById("captcha").Focus();//验证码
            }
            catch (Exception)
            {
            }
            this.timer1.Enabled = false;
        }

        //验 证码 是否，够位数，然后提交
        private void timSubmit_Tick(object sender, EventArgs e)
        {
            try
            {
                if (this.wb.Document.GetElementById("captcha").GetAttribute("value").Length >= 4)
                {
                    object oo = this.wb.Document.GetElementById("bn").InvokeMember("click");
                    this.timSubmit.Enabled = false;
                    //Thread.Sleep(1000);
                }
            }
            catch (Exception)
            {

            }
        }

        private void Frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            string s = "name:" + this.txtUserName.Text.Trim() + Environment.NewLine;
            s += "bir:" + this.dtpBir.Value.ToString("yyyy-MM-dd");
            VoteLog.Logs(s, fileName);
        }
    }
}
